package edu.winona.csclub.testing.utils;

import java.util.Random;

import junit.framework.Assert;

import org.junit.Test;

public class RangedRandom  {
	
	static Random r = new Random();
	
	long min,max;
	long diff;
	
	/**
	 * generates a random number generator that
	 * falls between 0 and 0, this will always return 0
	 */
	public RangedRandom(){}
	
	/**
	 * creates a random generator that will give you a random number between
	 * (and including) min and max
	 * @param min
	 * @param max
	 */
	public RangedRandom(int min, int max) {
		super();
		this.min = min;
		this.max = max;
		
		this.diff = ((this.max)-(this.min))+1;
	}

	public int nextInt(){
		long i = (long)((r.nextDouble()*diff)+min);
		return (int) i;
	}
	
	@Test
	public void testRangedRandom(){
		// basic test 
		int min =0;
		int max = 5;
		boolean minA=false,maxA=false;
		RangedRandom test = new RangedRandom(min,max);
		for( int i = 0; i < 10000 ; i ++){
			int t = test.nextInt() ;
			if(t< min || t > max){
				junit.framework.Assert.fail("A value occured outside given range");
			}
			if( t== min ){
				minA=true;
			}
			if( t== max ){
				maxA=true;
			}
		}
		if( !minA ){
			Assert.fail("min value never achieved");
		}
		if( !maxA ){
			Assert.fail("max value never achieved");
		}
	}
	
}
